package com.future.bantang.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.future.bantang.R;
import com.future.bantang.app.BanTangAPP;
import com.future.bantang.entity.BanTangHomeEntity.DataEntity.TopicEntity;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 16-1-13.
 */
public class LastestFieldListAdapter extends BaseAdapter<TopicEntity> {
    //更新数据时服务器返回的当前时间，单位：秒
    private int timeSecond=0;
    //如果timeSecond减去某个内容的发布时间小于此值，表示是在一天之内发布的，是新内容
    private static final int secondPerDay=86400;

    private ImageLoader mImageLoader;

    public LastestFieldListAdapter(Context context) {
        super(context);
        mImageLoader= BanTangAPP.getApp().getImageLoader();
    }

    public void setDatas(List<TopicEntity> list,int timeSecond) {
        this.timeSecond=timeSecond;
        super.setDatas(list);
    }

    public void addDatas(List<TopicEntity> list,int timeSecond) {
        this.timeSecond=timeSecond;
        super.addDatas(list);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        if (convertView==null){
            convertView= LayoutInflater.from(context)
                    .inflate(R.layout.fragment_home_body_list_item,null);
            holder=new ViewHolder();
            ViewUtils.inject(holder,convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        holder.setDatas(mList.get(position));
        return convertView;
    }

    private class ViewHolder {
        @ViewInject(R.id.iv_fragment_home_sub_img)
        private ImageView ivSubImg;
        @ViewInject(R.id.iv_fragment_home_sub_new)
        private ImageView ivSubNew;
        @ViewInject(R.id.tv_fragment_home_sub_title)
        private TextView tvSubTitle;
        @ViewInject(R.id.tv_fragment_home_sub_like_num)
        private TextView tvSubLikeNum;

        public void setDatas(TopicEntity entity){
            if (timeSecond-Integer.parseInt(entity.getUpdate_time())<secondPerDay){
                ivSubNew.setVisibility(View.VISIBLE);
            }else {
                ivSubNew.setVisibility(View.GONE);
            }
            tvSubTitle.setText(entity.getTitle());
            tvSubLikeNum.setText(entity.getLikes());
            String url= (String) ivSubImg.getTag();
            if (TextUtils.isEmpty(url)||!TextUtils.equals(url,entity.getPic())){
                ivSubImg.setTag(entity.getPic());
                mImageLoader.displayImage(entity.getPic(), ivSubImg);
            }

        }

    }
}
